#ifndef WORLD_H
#define WORLD_H

#include <QThread>
#include <QList>
#include <QMap>
#include <QTime>

#include "framemanager.h"
#include "streetmap.h"

class QGraphicsItem;
class TrafficControl;
class Vehicle;
class VehicleType;
class Driver;
class DriverType;


class World : public QThread
{
public:
  static World* instance();
  static bool destroyInstance();
  virtual void run();

  int loadMapXML(const QString &fileName);
  void clearMap();

  int loadTypesXML(const QString &fileName);
  int loadScenarioXML(const QString &fileName);
  int loadTrafficControlXML(const QString &fileName);
  void reloadScenario();
  void clearScenario();

  void setExiting(bool exiting);
  bool exiting();
  void tick();
  void addVehicle(Vehicle *v);
  Vehicle* addVehicle(const QString &name, const QString &typeName);
  Vehicle* findVehicle(const QString &name);
  void addDriver(Driver *d);
  Driver* addDriver(const QString &name, const QString &typeName);
  Driver* findDriver(const QString &name);

  static qreal gSimTimeStep;
  static QTime gSimTime;
  QList<VehicleType*> mVehicleTypes;
  QList<DriverType*> mDriverTypes;
  QList<Vehicle*> mVehicles;
  QList<Driver*> mDrivers;
  QMap<const StreetMap::Intersection*, TrafficControl*> mTrafficControls;
  StreetMap *mStreetMap;
  FrameManager mFrameManager;


private:
  World(); // singleton = private constructor
  ~World();

  static World *gInstance;

  bool mSimRunning;
  bool mExiting;
  QString mScenarioFilename;
  QString mMapFilename;
  QString mTrafficControlFilename;
  unsigned int mTotalTicks;
};

#endif // WORLD_H
